<template>
  <s-layout id="layout" :title="'离职审批'" navbar="normal" leftIconColor="#333" :autoBack="false" :bgColor="state.bgColor"
    :tabbarshow='false' :titleStyle="{ color: '#333', fontSize: '32rpx' }">
    <view class="call_on list_box" :style="{ paddingTop: (safeAreaInsets?.top + 44) + 'px' }">
      <fui-tabs :tabs="state.menulist" scroll @change="menulistclick"></fui-tabs>
      <scroll-view scroll-y class="comprehensive" v-if="list.length" @scrolltolower='listScrolltolower'
        show-scrollbar="true" :refresher-enabled="isOpenRefresh" :refresher-triggered="triggered"
        :refresher-threshold="100" refresher-background="white" @refresherpulling="onPulling"
        @refresherrefresh="onRefresh" @refresherrestore="onRestore" @refresherabort="onAbort" @scroll="onScroll"
        :style="state.status == '0,3' ? 'height:68vh' : 'height:82vh'"
        >
        <view class="item_wrap">
          <view class="item" v-for="(item, index) in list" :key="index">
            <view class="flex">
              <view class="select_image" @tap="moreSelect(item)" v-if="state.status == '0,3'">
                <image :src="item.is_selected ? `${baseUrl}/uploads/uniapp_image/staff/kehuguanli_icon_check.png` : `${baseUrl}/uploads/uniapp_image/staff/kehuguanli_icon_checkfor.png`
                  " mode="scaleToFill" />
              </view>
              <image class="flex_avater" :src="`${baseUrl}` + item.user.avatar" mode="scaleToFill" />
              <view class="item_left">
                <view class="item_left_box">
                  <text class="names">{{ item.user.nickname }}的离职申请</text>
                </view>
                <view class="item_left_box">
                  <text class="name">{{ item.user.group.name }}</text>
                </view>
              </view>
              <view class="item_right">
                <view :class="state.arrclass[item.state]">
                  {{ item.state_text }}
                </view>
              </view>
            </view>
            <view class="flex">

              <view class="item_left">
                <view class="item_left_box">
                  <view class="box_name">申请时间 </view>
                  <text class="box_content">{{ formatDate(item.createtime) }}</text>
                </view>
                <view class="item_left_box">
                  <view class="box_name">离职岗位 </view>
                  <text class="box_content">{{ item.cause }}</text>
                </view>
              </view>
              <view class="item_right" @click="lookDetail(item)">
                <text class="name">查看详情</text>
                <u-icon size="10" name="arrow-right" color="#0038F7"></u-icon>
              </view>
            </view>
          <view v-if="item.level == userInfo.id">
              <view class="buttons"  v-if="  item.state == 0 || item.state == 3">
                <button class="btn refuse" @click="refuse(item)">拒绝</button>
                <button class="btn blue" @click="agree(item)">同意</button>
              </view>
            </view>
          </view>

        </view>
        <!-- 正在加载组件 -->
        <view>
          <s-loadmore-gather :current_page="listPage" :last_page="listlast_page" ref="loadmoreRef"></s-loadmore-gather>
        </view>

      </scroll-view>

      <u-empty v-else mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" text="暂无内容"
        marginTop="50%"></u-empty>
        <!-- 底部占位盒子 -->
      <view v-if="state.status == '0,3'">

<view class="end_box" v-if="personLength !== 0">
  <!-- 全选按钮盒子 -->
  <view class="check_all_box">
    <!-- 全选按钮 -->
    <view class="check_all_button" @tap="checkAllInteraction">
      <view class="select_image">
        <image v-if="checkAll === true" :src="`${baseUrl}/uploads/uniapp_image/staff/kehuguanli_icon_check.png`"
          mode="scaleToFill" />
        <image v-else :src="`${baseUrl}/uploads/uniapp_image/staff/kehuguanli_icon_checkfor.png`"
          mode="scaleToFill" />
      </view>
      <view>全选</view>
    </view>


  </view>
  <view class="placeholder"></view>
   <!-- 底部保存按钮 -->
  <view>
    <s-button @leftNetWork="nextBackTowForm" @rightNetWork="nextTowForm" :leftWidth="'30%'" :rightWidth="'70%'"
      :type="'twobutton'" :oneText="'拒绝'" :twoText="'同意'" />

  </view>
</view>

</view>
    </view>
  </s-layout>
  <!-- 拒绝理由 -->
  <s-reasons-comfirm :show="showRefuse" @cancel="reasonsCancel" @affirm="reasonsConfirm"></s-reasons-comfirm>
</template>

<script setup>
import { baseUrl } from '@/sheep/config';
import sheep from '@/sheep';
import { onLoad, onShow } from "@dcloudio/uni-app";
import { ref, reactive } from 'vue';
import { formatDate } from '@/utils/fn.js';
const { safeAreaInsets } = uni.getSystemInfoSync()
const userInfo = sheep.$store('user').userInfo;
const state = reactive({
  bgColor: '#fff',
  menulist: [
    { name: '待审批', value: '0,3', badge: 0 },
    // { name: '待审批', value: '0', badge: 0 },
    { name: '已审批', value: '1' },
    { name: '已拒绝', value: '2' },
    { name: '审批中', value: '3' },
  ],
  currentTab: 0,
  status: '0,3',
  arrclass:['yellow','green','red','blue','gray','status']
})
//点击切换菜单  
const menulistclick = (item) => {
  list.value = [];
  state.status = item.value;
  listpagenumber.value = 1;
  selectedIds.value = []
  state.status = item.value;
  getListDetail()
}
const list = ref([])
const listPage = ref()
const listlast_page = ref();
const listpagenumber = ref(1);
// 全选
const checkAll = ref(false)
const selectedIds = ref([])
// 全选交互逻辑
const checkAllInteraction = () => {
  selectedIds.value = []
  if (checkAll.value === false) {
    list.value.forEach(item => {
      selectedIds.value.push(item.id);
      item.is_selected = true


    });
  } else {
    list.value.forEach(item => {
      item.is_selected = false

    });
  }
  checkAll.value = !checkAll.value
}
// 多选  total.value 总共多少条数据

const moreSelect = (item) => {
  item.is_selected = !(item?.is_selected)
  const index = selectedIds.value.indexOf(item.id);
  if (index === -1) {
    selectedIds.value.push(item.id);
    if (selectedIds.value.length == personLength.value) {
      checkAll.value = true
    }
  } else {
    selectedIds.value.splice(index, 1);
    if (selectedIds.value.length !== personLength.value) {
      checkAll.value = false
    }
  }
}
//全选拒绝
const nextBackTowForm = () => {
  
  if (selectedIds.value.length == 0) {
    sheep.$helper.toast('请选择待审批内容')
  } else {
    showRefuse.value = true
  }

}
// 全选同意
const nextTowForm = () => {

  if (selectedIds.value.length == 0) {
    sheep.$helper.toast('请选择待审批内容')
  } else {
    let params={
      ids:selectedIds.value
    }
    agreeList(params)

  }

}

onLoad((options) => {
  list.value = [];
  listpagenumber.value = 1;
  getListDetail()
})

// 1.
const loadmoreRef = ref()
//数据请求
const personLength = ref()
const getListDetail = () => {
  let obj = {
    state: state.status,
  };
  let state_op = '=';
  if(state.status == '0,3') {
    state_op = 'in';
  }
  let params = {
    filter: JSON.stringify(obj),
    op: JSON.stringify({
      state: state_op,
    }),
    page: listpagenumber.value,
    with: 'user'
  };
  sheep.$api.dimission.getDimissionApplyApproveList(params).then((res) => {
    list.value = [...list.value, ...res?.data.rows];
    listPage.value = res.data.current_page
    listlast_page.value = res.data.last_page
    // list.value.forEach(item => {
    //   if(item.level == userInfo.id) {
    //     if (item.state == 0) {
    //       state.menulist[0].badge = 1;
    //     } else if (item.state == 3) {
    //       state.menulist[3].badge = 1;
    //     }
    //   }
    // });
    if(state.status == '0,3') {
      state.menulist[0].badge = res.data.total
    }
    personLength.value = res.data.total
    return personLength.value
  });

}

//滚动触底
const listScrolltolower = () => {
  loadmoreRef.value.PopupScrolltolower()
  if (listPage.value < listlast_page.value) {
    listpagenumber.value += 1;
     checkAll.value = false
    selectedIds.value = []
    list.value.forEach(item => {
      item.is_selected = false
    })
  } else {
    return;
  }
  getListDetail();
};
const lookDetail = (item) => {
  sheep.$router.go('/subpackage/staff/workbench/dimission/dimission_detail', {
    ids: item.id,
   page:'approval'
  });
}


//同意
const agree = (item) => {

let params = {
  ids: item.id 

}
agreeList(params)


}
const agreeList = (params)=>{
uni.$u.throttle(() => {
// #ifdef MP
sheep.$platform.useProvider('wechat').subscribeMessage('apply_notice');
// #endif
sheep.$api.dimission.getDimissionPass(params).then((res) => {
  sheep.$helper.toast(res.msg);
  if (res.code == 1) {
    setTimeout(function () {
      list.value = [];
      listpagenumber.value = 1;
      checkAll.value = false
        selectedIds.value = []
      getListDetail();
    }, 1000);
  }
});


}, 1000);

}
// 拒绝
const showRefuse = ref(false)
const reasonsValue = ref()
const reasonsId = ref()
const refuse = (item) => {
  showRefuse.value = true
  reasonsId.value = item.id
}
//拒绝理由 确定 -->
const reasonsConfirm = (e) => {
  uni.$u.throttle(() => {
  // #ifdef MP
  sheep.$platform.useProvider('wechat').subscribeMessage('apply_result_notice');
  // #endif
  reasonsValue.value = e
  if (!reasonsValue.value) {
    sheep.$helper.toast('请输入拒绝理由');
    return;
  } else {
    let params = {
      ids: reasonsId.value || selectedIds.value,
      reasons: reasonsValue.value
    }
    sheep.$api.dimission.getDimissionRefuse(params).then((res) => {
      sheep.$helper.toast(res.msg)
      showRefuse.value = false
      reasonsValue.value = ''
      setTimeout(function () {
        list.value = []
        listpagenumber.value = 1
        checkAll.value = false
        selectedIds.value = []
        getListDetail()
      }, 1000);
    });
  }
}, 1000);
}
// 拒绝理由取消
const reasonsCancel = (e) => {
  showRefuse.value = e
  reasonsValue.value = ''
}
// 页面下拉刷新
const freshing = ref(false)
const triggered = ref(false)
const isOpenRefresh = ref(true)  // 是否开启下拉
// 自定义下拉刷新控件被下拉
const onPulling = (e) => {
  if (e.detail.deltaY < 0) return  // 防止上滑页面也触发下拉
  triggered.value = true;
}
// 自定义下拉刷新被触发
const onRefresh = () => {
  if (freshing.value) return;
  freshing.value = true;
  setTimeout(() => {
    list.value = []
    listpagenumber.value = 1
    checkAll.value =false
    selectedIds.value =[]
    getListDetail()
    triggered.value = false;
    freshing.value = false;

  }, 500);
}
// 自定义下拉刷新被复位
const onRestore = () => {
  triggered.value = 'restore'; // 需要重置
  // console.log("onRestore");
}
// 自定义下拉刷新被中止
const onAbort = () => {
  // console.log("onAbort");
}
</script>
<style lang="scss" scoped>
@import '../compentns/publiclist.scss';

// 滚动
.comprehensive {
  width: calc(100% - 40rpx);
  background-color: rgba(0, 0, 0, 0);
  margin: 0 20rpx;
  height: 88vh;
}

.buttons {
  border: none !important;
}

.flex {
  display: flex;
  border-bottom: 1rpx solid #efefef;
  margin-bottom: 10rpx;

  &:nth-child(2) {
    .item_left {
      width: 76% !important;
    }
  }

  &:last-child {
    border-bottom: none;
  }

  .flex_avater {
    border-radius: 40rpx;
    width: 80rpx;
    height: 80rpx;
    margin: 20rpx 0 0 30rpx;
  }

  .item_left_box {
    .names {
      color: $uni-text-color;
      font-weight: 500;
      font-size: $uni-font-size-big;
    }

    .name {
      color: #999999;
      font-weight: 500;
      font-size: $uni-font-size-sm;
    }
  }

  .item_right {
    margin-top: 50rpx !important;

    .status {
      width: 104rpx;
      height: 36rpx;
      font-family: PingFangSC;
      color: $uni-color-primary;
      font-weight: 500;
      font-size: 22rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 18rpx;
      margin-right: 18rpx;
      background-color: rgba(0, 56, 247, 0.1);
    }

    .blue {
      width: 104rpx;
      height: 36rpx;
      font-family: PingFangSC;
      color: $uni-color-primary;
      font-weight: 500;
      font-size: 22rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 18rpx;
      margin-right: 18rpx;
      background-color: rgba(0, 56, 247, 0.1);
    }

    .gray {
      width: 104rpx;
      height: 36rpx;
      font-family: PingFangSC;
      color: #333;
      font-weight: 500;
      font-size: 22rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 18rpx;
      margin-right: 18rpx;
      background-color: #efefef;
    }

    .yellow {
      width: 104rpx;
      height: 36rpx;
      font-family: PingFangSC;
      color: #F77704;
      font-weight: 500;
      font-size: 22rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 18rpx;
      margin-right: 18rpx;
      background-color: #FEF3E6;
    }

    .green {
      width: 104rpx;
      height: 36rpx;
      font-family: PingFangSC;
      color: #17A500;
      font-weight: 500;
      font-size: 22rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 18rpx;
      margin-right: 18rpx;
      background-color: #EAFEE6;
    }

    .red {
      width: 104rpx;
      height: 36rpx;
      font-family: PingFangSC;
      color: #FF0000;
      font-weight: 500;
      font-size: 22rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 18rpx;
      margin-right: 18rpx;
      background-color: #FEE6E6;
    }
  }

  .item_left_box {
    margin-bottom: 10rpx;
  }

  .box_name {
    color: $uni-text-color;
    font-weight: 500;
   font-size:$uni-font-size-26;
    margin-right: 18rpx;
  }

  .box_content {
    font-family: PingFangSC;
    color: #666666;
    font-weight: 400;
   font-size:$uni-font-size-26;
    white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      height: 30rpx;
  }
}

.item {
  padding-bottom: 10rpx;

  .item_left_box {
    margin-bottom: 10rpx !important;
  }

  .flex {
    align-items: center !important;
  }

  .item_left {
    width: 55% !important;
  }

  .item_right {
    padding-right: 10rpx !important;
  }
}

// 滚动
.comprehensive {
  width: calc(100% - 40rpx);
  background-color: rgba(0, 0, 0, 0);
  margin: 0 20rpx;
  height: 68vh;
}

// 选择图标
.select_image {
  width: 30rpx;
  height: 30rpx;
  margin: 0 22rpx;

  image {
    width: 100%;
    height: 100%;
  }
}

// 末尾盒子 (此盒子没有  “父相 ” 如要改动请按需更改)
.end_box {
  position: fixed;
  bottom: 0;
  z-index: 98;
  height: 244rpx;
  width: 100%;

  // 全选按钮盒子
  .check_all_box {
    height: 80rpx;
    width: 100%;
    background-color: #ffffff;
    display: flex;
    justify-content: space-between;
    border: 2rpx solid #efefef;

    // 全选按钮
    .check_all_button {
      display: flex;
      margin-left: 52rpx;
      align-items: center;
      font-family: PingFangSC;
      color: #0239f7;
      font-weight: $uni-font-weight;
      font-size: $uni-font-size-base;
      font-style: normal;
      text-decoration: none;
    }

    // 已选择
    .selected_box {
      display: flex;
      margin-right: 40rpx;
      align-items: center;
      font-family: PingFangSC;
      color: $uni-text-color;
      font-weight: $uni-font-weight;
      font-size: $uni-font-size-base;
      font-style: normal;
      text-decoration: none;
    }
  }

  .supervisor_state {
    display: flex;
    margin-left: 52rpx;
    align-items: center;
    font-family: PingFangSC;
    color: #0239f7;
    font-weight: $uni-font-weight;
    font-size: $uni-font-size-base;
    font-style: normal;
    text-decoration: none;
  }

  // 占位盒子
  .placeholder {
    height: 32rpx;
    width: 100%;
    background-color: #ffffff;
  }
}


</style>